/**
 * @author htl
 * @date 2018/12/19 0019
 * @Description:
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const wechatSecMenuSchema = new Schema({
    pid: {
        type: String,  // 父级主键
        required: true,
    },
    type: {
        type: String,  // 类型，click点击、view跳转链接
        required: true,
        enum: ['click','view']
    },
    name: {
        type: String,  // 按钮名称
        required: true,
    },
    key: {
        type: String,  // 用于type为click时，点击数据的标识
    },
    url: {
        type: String,  // 用户view，跳转的url
    },
}); //一级菜单
wechatSecMenuSchema.statics = {
    findDocs: function (findObj={},showHandle,handleObj,cb) {
        return new Promise((resolve,reject) => {
            this.find(findObj,showHandle,handleObj,(err,docs) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                }else {
                    resolve(docs);
                }
            })
        })

    },
    deleteDocs: function (deletebj,cb) {
        return new Promise((resolve,reject) => {
            this.remove(deletebj,(err,docs) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                }else {
                    resolve(true);
                }
            })
        })

    },
    updateDocs: function (updateOpt,updateObj,cb) {
        return new Promise((resolve,reject) => {
            this.updateOne(updateOpt,updateObj,(err,docs) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                }else {
                    resolve(true);
                }
            })
        })

    },
    addDocs: function(addObj) {
        return new Promise((resolve,reject) => {
            this.create(addObj,(err,docs) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                }else {
                    resolve(true);
                }
            })
        })
    },
    getCount: function() {
        return new Promise((resolve,reject) => {
            this.count((err,num) => {
                if(err) {
                    return resolve(err);
                }
                resolve(num);
            })
        })
    }
}

wechatSecMenuSchema.methods.addDocs = function(cb) {
    this.save((err,docs) => {
        if(err) {
            cb(9999);
        }else {
            cb(true);
        }
    })
}
const wechatSecMenuModel = mongoose.model('wechatSecMenu',wechatSecMenuSchema); //创建集合（表）


module.exports = wechatSecMenuModel;
